'use strict';

const Service = require('egg').Service;
const { Op } = require('sequelize');
const moment = require("moment");

class UserLikeService extends Service {

    async findByType(userId, type) {
        return await this.ctx.model.UserLike.findAll({
            include: [
                {
                    model: this.app.model.User,
                    as: 'user'
                },
                {
                    model: this.app.model.User,
                    as: 'userLike'
                }
            ],
            where: {
                user_id: userId,
                like: {
                    [Op.eq]: type
                },
                update_time: {
                    [Op.gt]: moment().format("YYYY-MM-DD")
                }
            }
        })
    }

    async findByUserId(userId) {
        let now = new Date();
        now.setDate(now.getDate() - 1);
        return await this.ctx.model.UserLike.findAll({
            where: {
                user_id: userId,
                update_time: {
                    [Op.gt]: now
                }
            }
        })
    }

    async getUserLike(userId) {
        const user = await this.findByUserId(userId);
        let like = '0';
        if (user) {
            user.forEach(u => {
                console.log(u);
                like = like + ',' + u.like_user_id;
            })
        }
        return like;
    }
}

module.exports = UserLikeService;